package com.sudichina.sudichina.view.activity.setting;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sudichina.sudichina.Contains;
import com.sudichina.sudichina.MyApp;
import com.sudichina.sudichina.NET.Api;
import com.sudichina.sudichina.NET.NetUtils;
import com.sudichina.sudichina.NET.onRequestDataFinish;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.bean.Result;
import com.sudichina.sudichina.bean.User;
import com.sudichina.sudichina.utils.DESMD5Utils;
import com.sudichina.sudichina.utils.SpUtils;
import com.sudichina.sudichina.utils.Toastutils;
import com.sudichina.sudichina.utils.Util;
import com.sudichina.sudichina.view.activity.main.BaseActivity;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.sudichina.sudichina.NET.Api.API_LOGIN;

public class ChangeLoginPswActivity extends BaseActivity implements onRequestDataFinish {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.et_aclp_orignpsw)
    EditText etAclpOrignpsw;
    @BindView(R.id.et_aclp_newpsw)
    EditText etAclpNewpsw;
    @BindView(R.id.bt_aclp_next)
    Button btAclpNext;
    @BindView(R.id.activity_change_phone_number)
    LinearLayout activityChangePhoneNumber;
    private boolean isPsw = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_login_psw);
        ButterKnife.bind(this);
        tvTitle.setText("修改登录密码");
    }

    @OnClick({R.id.iv_back, R.id.bt_aclp_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_aclp_next:

                String Orignpsw = etAclpOrignpsw.getText().toString();
                String Newpsw = etAclpNewpsw.getText().toString();
                if (Orignpsw.equals(Newpsw)) {
                    Toastutils.showToast("两次密码一致,\n请重新输入新的密码");
                    return;
                }
                if (!Util.IsPassword(Newpsw)) {
                    Toastutils.showToast("密码由数字和字母组成,\n长度为6-16位");
                    return;
                }

                //验证原来的密码是否正确
                identifyingPsw(Orignpsw);


                break;
        }
    }

    private void identifyingPsw(String orignpsw) {

        HashMap<String, Object> map = new HashMap<>();
        String accountName = MyApp.getCurrentUser().getAccountName();
        map.put("accountName", accountName);
        map.put("accountPassword",DESMD5Utils.MD5(orignpsw, false));
        Type type = new TypeToken<Result<User>>() {
        }.getType();
        NetUtils.requestData(API_LOGIN, map, this, type, false);
        Toastutils.showToast("发送了登录请求\n~~~~~~~~~~~");




    }

    private void ResetPsw(String newpsw) {

        HashMap<String, Object> map = new HashMap<>();
/*
accountName: String 手机号
accountPassword：String 密码  MD5加密

accountName: String 手机号
 */

        User currentUser = MyApp.getCurrentUser();
        String phone = currentUser.getAccountName();
        String md5_pwd = DESMD5Utils.MD5(newpsw, false);
        map.put("accountName", phone);
        map.put("accountPassword", md5_pwd);

        Type type = new TypeToken<Result>() {
        }.getType();


        NetUtils.requestData(Api.FORGOT, map, this, type, false);


    }

    @Override
    public void requestdataFinish(String catalog, Object bean) {
        switch (catalog) {
            case Api.API_LOGIN:
                Result<User> bean1 = (Result<User>) bean;
                int code = bean1.getCode();
             User user = bean1.getData();


             String msg = bean1.getMsg();


                if (code == 8004) {
                    Toastutils.showToast("密码错误");
                    return;
                }
                if (code == 1) {
                    //保存为全局变量
                    MyApp.setCurrentUser(user);
                    ResetPsw(etAclpNewpsw.getText().toString());
                    return;
                }

                break;

            case Api.FORGOT:
                Result bean2 = (Result) bean;
                int code2 = bean2.getCode();
//             User user = bean1.getData();
//
//
//             String msg = bean1.getMsg();
//             System.out.println("code: " + code + "data:" + user + "msg:" + msg);

                if (code2 == 8009) {
                    Toastutils.showToast("密码重置失败");
                    return;
                }
                if(code2==1) {
                    Toastutils.showToast("密码修改成功");
                    SpUtils.putString(this, Contains.PASSWORD,DESMD5Utils.MD5(etAclpNewpsw.getText().toString(),false));

                    finish();
                }
                break;
        }


    }

    @Override
    public void requestdataFailed(IOException e) {
        Toastutils.showToast("IOException" + e);

    }
}
